//
//  NSRegularExpression+SHY_GetValues.m
//  NSRegularExpressionGetValues
//
//  Created by Shenry on 2017/10/19.
//  Copyright © 2017年 Shenry. All rights reserved.
//

#import "NSRegularExpression+SHY_GetValues.h"

@implementation NSRegularExpression (SHY_GetValues)

+ (BOOL)isMatchRegularExpression:(NSString *)regex checkString:(NSString *)checkString {
    if (!checkString) {
        return NO;
    }
    NSError *error = NULL;
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regex
                                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                                         error:&error];
    
    NSTextCheckingResult *result =
    [regularExpression firstMatchInString:checkString options:NSMatchingReportProgress range:NSMakeRange(0, [checkString length])];
    return result != nil;
}

+ (NSString *)partStringOfCheckStringWithRegularExpression:(NSString *)regex checkString:(NSString *)checkString {
    if (!checkString) {
        return nil;
    }
    NSError *error = NULL;
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regex
                                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                                         error:&error];
    NSTextCheckingResult *result =
    [regularExpression firstMatchInString:checkString options:NSMatchingReportProgress range:NSMakeRange(0, [checkString length])];
    return result ? [checkString substringWithRange:result.range] : nil;
}

+ (NSArray<NSString *> *)arrayOfCheckStringWithRegularExpression:(NSString *)regex checkString:(NSString *)checkString {
    if (!checkString) {
        return nil;
    }
    NSString *str111=[checkString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSError *error = NULL;
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regex
                                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                                         error:&error];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
    NSArray *results = [regularExpression matchesInString:str111 options:0 range:NSMakeRange(0, str111.length)];
    for (NSTextCheckingResult *item in results) {
        NSString *str222=[NSString stringWithFormat:@"%@%@%@",@"@interface",[str111 substringWithRange:item.range],@"@end"];
        [arr addObject:str222];
    }
    
    return [arr copy];
}

+ (NSArray<NSString *> *)arrayOfCheckStringWithRegularExpression2:(NSString *)regex checkString:(NSString *)checkString {
    if (!checkString) {
        return nil;
    }
    NSString *str111=[checkString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSError *error = NULL;
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regex
                                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                                         error:&error];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
    NSArray *results = [regularExpression matchesInString:str111 options:0 range:NSMakeRange(0, str111.length)];
    for (NSTextCheckingResult *item in results) {
        NSString *str222=[NSString stringWithFormat:@"%@%@%@",@"@interface",[str111 substringWithRange:item.range],@"@end"];
        [arr addObject:str222];
    }
    
    return [arr copy];
}

@end
